package com.yunc.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunc.base.exception.NoShopException;
import com.yunc.base.utils.ReflexUtils;
import com.yunc.base.utils.SecurityUtils;
import com.yunc.base.utils.SpringContextUtils;
import com.yunc.modules.shop.entity.Shop;
import com.yunc.modules.shop.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author taocongcong
 * @create 2020-01-20 10:33
 */
public class ShopBaseController {

    @Autowired
    private IShopService shopService;

    public void setShop(QueryWrapper qw) {
        String userId = SecurityUtils.getUserId();
        Shop shop = shopService.getByUserId(userId);
        if (shop == null) {
            throw new NoShopException("需要有店铺");
        }
        qw.eq("shop_id", shop.getId());
    }

    public void setShopId(Object t) {
        String userId = SecurityUtils.getUserId();
        Shop shop = shopService.getByUserId(userId);
        if (shop == null) {
            throw new NoShopException("需要有店铺");
        }
        // 设置传入id
        ReflexUtils.setFieldValueByFieldName("shopId", shop.getId(), t, Long.class);
    }

}
